package com.yygo.webapp.controller;

import com.yygo.Constants;
import com.yygo.model.LabelValue;
import com.yygo.service.CertificateManager;
import org.apache.commons.lang.StringUtils;
import org.appfuse.service.GenericManager;
import com.yygo.model.Certificate;
import com.yygo.webapp.controller.BaseFormController;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

@Controller
@RequestMapping("/certificateform*")
public class CertificateFormController extends BaseFormController {

    @Autowired
    private CertificateManager certificateManager;

    public CertificateFormController() {
        setCancelView("redirect:certificates");
        setSuccessView("redirect:certificates");
    }

    @ModelAttribute
    @RequestMapping(method = RequestMethod.GET)
    protected Certificate showForm(HttpServletRequest request)
    throws Exception {
        String id = request.getParameter("id");

        if (!StringUtils.isBlank(id)) {
            return certificateManager.get(new Long(id));
        }

        return new Certificate();
    }

    @RequestMapping(method = RequestMethod.POST)
    public String onSubmit(Certificate certificate, BindingResult errors, HttpServletRequest request,
                           HttpServletResponse response)
    throws Exception {
        if (request.getParameter("cancel") != null) {
            return getCancelView();
        }

        if (validator != null) { // validator is null during testing
            validator.validate(certificate, errors);

            if (errors.hasErrors() && request.getParameter("delete") == null) { // don't validate when deleting
                return "certificateform";
            }
        }

        log.debug("entering 'onSubmit' method...");

        boolean isNew = (certificate.getId() == null);
        String success = getSuccessView();
        Locale locale = request.getLocale();

        if (request.getParameter("delete") != null) {
            certificateManager.remove(certificate.getId());
            saveMessage(request, getText("certificate.deleted", locale));
        } else {
            certificateManager.save(certificate);
            String key = (isNew) ? "certificate.added" : "certificate.updated";
            saveMessage(request, getText(key, locale));

            if (!isNew) {
                success = "certificateform";
            }
        }

        return success;
    }

    @ModelAttribute("availableTypes")
    protected List<LabelValue> loadAvailableTypes() {
        String text = getText("CERTIFICATE_TYPE", Locale.ENGLISH);
        List<LabelValue> list = new ArrayList<LabelValue>();
        String[] values = text.split("(\\s)*,(\\s)*");
        for (String value: values) {
            String label = getText("CERTIFICATE_TYPE_" + value, LocaleContextHolder.getLocale());
            list.add(new LabelValue(label, value));
        }

        return list;
    }
}
